//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    page:0,
    showLoading: true,
    noMoretip: false,
    status:1
  },
  onLoad: function () {
    var that =this;
    app.setNavtitle('我的参与');
  },
  onShow: function () {
    var that = this;
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    }
    that.requestBargainlist();
  },
  requestBargainlist: function () {
    var that = this;
    var page = that.data.page;
    var status = that.data.status;
    var data = {
      map: 'applet_bargain_my_activity',
      page: page,
      status: status
    };
    console.log(data);
    //发起请求，获取列表列表
    // wx.showLoading({
    //   title: '加载中',
    // })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.bargainGoodslist;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            bargainGoodslist: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              bargainGoodslist: [],
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        // wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    this.onShow();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestBargainlist();
    }
  },
  toggleStatus:function(e){
    var that = this;
    var status = e.target.dataset.status;
    that.setData({
      status: status,
      bargainGoodslist:null
    })
    that.onPullDownRefresh();
  },
  tobargainGoodDetail: function (e) {//砍价商品详情
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages/bargainGoodDetail/bargainGoodDetail?id=' + id
    })
  },
  toCuslink: function (e) {
    var link = e.currentTarget.dataset.link;
    console.log(link);
    wx.navigateTo({
      url: link
    })
  }
})